/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager.create;

import br.com.deployment.daoImpl.ExecutorDAOImpl;
import br.com.deployment.entities.Address;
import br.com.deployment.entities.Executor;
import br.com.deployment.entities.Organ;
import br.com.deployment.utils.Msg;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author ely
 */
@ManagedBean
@SessionScoped
public class ManagerCreateExecutor {

    @EJB
    ExecutorDAOImpl executorService;
    
    // Objeto para criar o Executor
    private Executor executor;
    // Objeto para criar o Endereço
    private Address  address;
    
    private Organ organ;
    // Confimar Senha
    private String   confirma;

    public ManagerCreateExecutor() {
        this.organ = new Organ();
    }

    @PostConstruct
    public void init() {
        executor = new Executor();
        address  = new Address();
    }
    // Reinicializa os Objetos
    public void reinit(){
        executor = new Executor();
        address  = new Address();
    }
    
    // Salva o Objeto Executor
    public void save() {
        // Verificando se Exitem campos Vazios
        if (executor.getLogin().equals("") || executor.getPassword().equals("")
                || executor.getEmail().equals("")) {
            Msg.messagemWarn("Campos: Email, Login e Senha são obrigatórios");
        } else {
            // Verifica e o atributo confirma é igual ao atributor senha
            if (confirma.equals(executor.getPassword())) {
                executor.setPassword(Executor.encryptPassword(executor.getPassword()));
                executor.setAddress(address);
                executor.setOrgan(organ);
                executorService.save(executor);
                Msg.messagemInfo("Juíz Cadastrado com Sucesso");
                reinit();
            } else {
                Msg.messagemError("Senhas não conferem");
            }
        }
    }

    // Ato complemento para o Executor
    public List<Executor> completeExecutor(String query) {
        List<Executor> bd = new ArrayList<Executor>();

        Executor e = new Executor();
        e.setName(query);
        bd = executorService.autoCompleteExecutor(e);
        return bd;
    }

    public Executor getExecutor() {
        return executor;
    }

    public void setExecutor(Executor executor) {
        this.executor = executor;
    }

    public String getConfirma() {
        return confirma;
    }

    public void setConfirma(String confirma) {
        this.confirma = confirma;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Organ getOrgan() {
        return organ;
    }

    public void setOrgan(Organ organ) {
        this.organ = organ;
    }
    
    
    
}
